/**
 * 
 */
package com.handinfo.game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.engine.resource.ObjectAdapter;
import com.handinfo.engine.resource.ResHead;
import com.handinfo.engine.resource.ResourceManager;
import com.handinfo.engine.resource.SerializeableImage;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UITools;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.uimenu.window.UIAuction;
import com.handinfo.uimenu.window.UIBag;
import com.handinfo.uimenu.window.UIMailList;
import com.handinfo.uimenu.window.UIMenu;
import com.handinfo.uimenu.window.UISetShortCut;
import com.handinfo.uimenu.window.UISkill;
import com.handinfo.uimenu.window.UISocialWindow;
import com.handinfo.util.Tools;

/**
 * @author venizeng
 * 
 * 功能菜单
 */
public class MenuFunction
{
	public static final short ID_ATT = 1;// 个人属性
	public static final short ID_BAG = 2;// 背包
	public static final short ID_TASK = 3;// 任务列表
	public static final short ID_DONGFU = 4;// 洞府
	public static final short ID_CHAT = 5;// 聊天
	public static final short ID_MAIL = 6;// 查看信息
	public static final short ID_FRIEND = 7;// 查看好友
	public static final short ID_MAP = 8;// 地图
	public static final short ID_SIDE_PLAYERS = 9;// 附近玩家
	public static final short ID_MYTEAM = 10;// 我的队伍
	public static final short ID_YUANBAO_MARKET = 11;// 元宝,商城
	public static final short ID_YUANBAO_CHONGZHI = 12;// 元宝,充值
	public static final short ID_HUODONG = 13;// 活动
	public static final short ID_AUCTION = 14;// 拍卖
	public static final short ID_SET_SHORTCUT = 15;// 快捷栏
	public static final short ID_LEARN_SKILL = 16;// 技能
	public static final short ID_SIDE_TEAM = 17;// 附近队伍
	public static final short ID_JOIN_TEAM = 18;// 加入队伍
	public static final short ID_ADD_FRIEND = 19;// 添加好友
	public static final short ID_ADD_ENEMY = 20;// 添加仇敌
	public static final short ID_VIEWPRIORITY = 21;//显示优化级
	public static final short ID_RIDE = 22;// 骑宠
	public static final short ID_MARCO1 = 23;// 宏1
	public static final short ID_MARCO2 = 24;// 宏2
	public static final short ID_MARCO3 = 25;// 宏3
	public static final short ID_MARCO4 = 26;//宏4
	public static final short ID_GONGLUE = 27;//攻略
	public static final short ID_ONE_KEY_TASK = 28;//一键任务
	//
	public static final String STR_ATT = "查看属性";// 
	public static final String STR_BAG = "查看背包";// 背包
	public static final String STR_TASK = "查看任务";// 任务列表
	public static final String STR_DONGFU = "查看洞府";// 洞府
	public static final String STR_CHAT = "聊天频道";// 聊天
	public static final String STR_MAIL = "查看邮件";// 查看信息
	public static final String STR_FRIEND = "查看好友";//
	public static final String STR_MAP = "查看地图";// 地图
	public static final String STR_SIDE_PLAYERS = "附近玩家";// 附近玩家
	public static final String STR_MYTEAM = "我的队伍";// 
	public static final String STR_YUANBAO_MARKET = "元宝商城";// 元宝,商城
	public static final String STR_YUANBAO_CHONGZHI = "元宝充值";// 元宝,充值
	public static final String STR_HUODONG = "查看活动";// 活动
	public static final String STR_AUCTION = "拍卖";// 拍卖
	public static final String STR_SET_SHORTCUT = "设置快捷栏";// 快捷栏
	public static final String STR_LEARN_SKILL = "查看技能";// 技能
	public static final String STR_SIDE_TEAM = "附近队伍";// 
	public static final String STR_JOIN_TEAM = "加入队伍";// 
	public static final String STR_ADD_FRIEND = "添加好友";// 
	public static final String STR_ADD_ENEMY = "添加仇敌";// 
	public static final String STR_VIEWPRIORITY = "显示优先级";// 
	private static final String STR_RIDE = "骑宠";
	public static final String STR_MARCO1 = "连招1";// 宏1
	public static final String STR_MARCO2 = "连招2";// 宏2
	public static final String STR_MARCO3 = "连招3";// 宏3
	public static final String STR_MARCO4 = "连招4";//宏4
	public static final String STR_GONGLUE = "攻略";//宏4
    public static final String STR_ONE_KEY_TASK = "一键任务";
	//
	//
	public static Image s_icon;
	private short m_iconId;
	public short m_id;// 功能Id

	public MenuFunction(long id)
	{
		setId(id);
	}

	static {
		ResHead head = new ResHead(ObjectAdapter.RESOURCE_TYPE_PNG, UIManager.RES_ACTORITEM, 1);
		s_icon = ((SerializeableImage) ResourceManager
				.getInstance()
				.loadResource(GameManager.getInstance().m_resource_static, head)).m_image;
	}

	public static MenuFunction[] cretaeMenus()
	{
		short[] ids = new short[] { ID_ATT, ID_BAG, ID_TASK, ID_DONGFU, ID_CHAT, ID_MAIL, ID_FRIEND, ID_MAP,
				ID_SIDE_PLAYERS
				, ID_MYTEAM, ID_HUODONG, ID_SET_SHORTCUT, ID_LEARN_SKILL, ID_JOIN_TEAM, ID_ADD_FRIEND, ID_ADD_ENEMY,
				ID_VIEWPRIORITY, ID_MARCO1, ID_MARCO2, ID_MARCO3, ID_MARCO4 ,ID_GONGLUE,ID_ONE_KEY_TASK};
		MenuFunction[] menus = new MenuFunction[ids.length];
		for (int i = 0; i < ids.length; i++) {
			menus[i] = new MenuFunction(ids[i]);
		}
		return menus;
	}

	/**
	 * 设置ID 2012-3-23 下午02:57:22 venizeng
	 * @param id
	 */
	private void setId(long id)
	{
		m_id = (short) id;
		switch (m_id) {
			case ID_DONGFU:// 洞府
				m_iconId = 91;
				break;
			case ID_CHAT:// 聊天记录
				m_iconId = 92;
				break;
			case ID_FRIEND:// 查看好友
				m_iconId = 93;
				break;
			case ID_SIDE_PLAYERS:// 附近玩家
				m_iconId = 94;
				break;
			case ID_MYTEAM:// 我的队伍
				m_iconId = 95;
				break;
			case ID_YUANBAO_MARKET://
				m_iconId = 96;
				break;
			case ID_YUANBAO_CHONGZHI://
				m_iconId = 97;
				break;
			case ID_HUODONG://
				m_iconId = 98;
				break;
			case ID_AUCTION://
				m_iconId = 99;
				break;
			case ID_LEARN_SKILL:
				m_iconId = 100;
				break;//
			case ID_SIDE_TEAM:
				m_iconId = 101;
				break;//
			case ID_ADD_FRIEND:
				m_iconId = 102;
				break;//
			case ID_JOIN_TEAM:
				m_iconId = 103;
				break;//
			case ID_SET_SHORTCUT://
				m_iconId = 104;
				break;
			case ID_ADD_ENEMY:
				m_iconId = 105;
				break;//
			case ID_ATT:// 个人属性
				m_iconId = 7;
				break;
			case ID_BAG:// 背包
				m_iconId = 9;
				break;
			case ID_TASK:// 任务列表
				m_iconId = 1;
				break;
			case ID_MAIL:// 查看信息
				m_iconId = 11;
				break;
			case ID_MAP:// 地图
				m_iconId = 2;
				break;
			case ID_VIEWPRIORITY:
				m_iconId = 14;//测试用
				break;
			case ID_RIDE:
				m_iconId = 77;
				break;
			case ID_MARCO1:
			case ID_MARCO2:
			case ID_MARCO3:
			case ID_MARCO4:
				m_iconId = 82;
				break;
			case ID_GONGLUE:
				m_iconId = 17 ;
				break ;
			case ID_ONE_KEY_TASK:
				m_iconId = 90;
				break;
		}
	}

	public int getId()
	{
		return m_id;
	}

	/**
	 * 绘制功能按钮 2012-3-19 下午05:09:11 venizeng
	 * @param g
	 * @param iconId
	 */
	public void drawIcon(Graphics g, int x, int y)
	{
		// ///游戏场景中用到的各图标均使用GameScene中的s_img_icon
		short id = (short) (m_iconId - 1);
		AbstractGameScreen.drawRegion(g, GameScene.s_img_icon,
				(id % GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[0],
				(id / GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[1],
				GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1], 0, x, y, Tools.LEFT_TOP);

		//
		switch (m_id) {
			case ID_MARCO1:
				drawMarco(g, x, y, 1);
				break;
			case ID_MARCO2:
				drawMarco(g, x, y, 2);
				break;
			case ID_MARCO3:
				drawMarco(g, x, y, 3);
				break;
			case ID_MARCO4:
				drawMarco(g, x, y, 4);
				break;
		}
	}

	/**
	 * 绘制宏标签 2012-4-5 上午10:59:08 venizeng
	 * @param g
	 * @param x
	 * @param y
	 */
	public void drawMarco(Graphics g, int x, int y, int index)
	{
		int w = GameScene.SIZE_IMG_ICON[0];
		int h = GameScene.SIZE_IMG_ICON[1];
		if (index == -1) {
			return;
		}
		Tools.drawNumber(g, GameScene.s_img_num, x + w, y + h, UITools.W_NUM, UITools.H_NUM, index, 0,
				Tools.RIGHT_BOTTOM, false);
	}

	/**
	 * 2012-3-23 下午03:16:06 venizeng
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	public void drawDescribe(Graphics g, int x, int y)
	{
		String temp = getName();
		if (temp == null) {
			return;
		}
		int IH_DESCRIBE = 3;// 框的间隙高度
		int h = AbstractGameScreen.FONT_H + (IH_DESCRIBE << 1);
		int w = 100;
		UITools.drawRect(g, x, y, w, h, 0x202221, 0);
		UITools.drawRect(g, x, y, w, h, 0xefef8c, 1);
		UITools.drawRect(g, x, y, w, h, 0x18424a, 2);
		UITools.fillRect(g, x, y, w, h, 0x8c6b5a, 3);
		g.setColor(0xffef8b);
		int tempX = x + IH_DESCRIBE;
		int tempY = y + IH_DESCRIBE;
		g.drawString(temp, tempX, tempY, 0);
	}

	private String getName()
	{
		switch (m_id) {
			case ID_ATT:// 个人属性
				return STR_ATT;
			case ID_BAG:// 背包
				return STR_BAG;
			case ID_TASK:// 任务列表
				return STR_TASK;
			case ID_DONGFU:// 洞府
				return STR_DONGFU;
			case ID_CHAT:// 聊天记录
				return STR_CHAT;
			case ID_MAIL:// 查看信息
				return STR_MAIL;
			case ID_FRIEND:// 查看好友
				return STR_FRIEND;
			case ID_MAP:// 地图
				return STR_MAP;
			case ID_SIDE_PLAYERS:// 附近玩家
				return STR_SIDE_PLAYERS;
			case ID_MYTEAM:// 我的队伍
				return STR_MYTEAM;
			case ID_YUANBAO_MARKET://
				return STR_YUANBAO_MARKET;
			case ID_YUANBAO_CHONGZHI://
				return STR_YUANBAO_CHONGZHI;
			case ID_HUODONG://
				return STR_HUODONG;
			case ID_AUCTION://
				return STR_AUCTION;
			case ID_SET_SHORTCUT://
				return STR_SET_SHORTCUT;
			case ID_LEARN_SKILL:
				return STR_LEARN_SKILL;
			case ID_SIDE_TEAM:
				return STR_SIDE_TEAM;
			case ID_JOIN_TEAM:
				return STR_JOIN_TEAM;
			case ID_ADD_FRIEND:
				return STR_ADD_FRIEND;
			case ID_ADD_ENEMY:
				return STR_ADD_ENEMY;
			case ID_VIEWPRIORITY:
				return STR_VIEWPRIORITY;
			case ID_RIDE:
				return STR_RIDE;
			case ID_MARCO1:
				return STR_MARCO1;
			case ID_MARCO2:
				return STR_MARCO2;
			case ID_MARCO3:
				return STR_MARCO3;
			case ID_MARCO4:
				return STR_MARCO4;
			case ID_GONGLUE:
				return STR_GONGLUE ;
			case ID_ONE_KEY_TASK:
				return STR_ONE_KEY_TASK;
			default:
				return null;
		}
	}

	/**
	 * 2012-3-19 下午07:13:18 venizeng
	 * @param id
	 */
	public static void doAction(long id)
	{
		switch ((short) id) {
			case ID_ATT:// 个人属性
				if (UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_ATTRIBUTE) == null) {
					GameManager.getInstance().m_game_message.sendAttInfos(GameManager.getInstance().m_role.m_gameID);
				}
				break;
			case ID_BAG:// 背包
				if (UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_BAG) == null) {
					UIBag.gotoNormal();
				}
				break;
			case ID_TASK:// 任务列表
				GameManager.getInstance().m_game_message.sendTaskRoleList();
				break;
			case ID_DONGFU:// 洞府
				GameManager.getInstance().m_game_message.sendDongFuInto(GameManager.getInstance().m_role.m_gameID);
				break;
			case ID_CHAT:// 聊天记录
				if (UIManager.getInstance().getWindowById(UIWindowFactory.WINDOW_CHAT) == null) {
					UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_CHAT);
				}
				break;
			case ID_MAIL:// 查看信息
				UIMailList.openMailMenu();
				break;
			case ID_FRIEND:// 查看好友
				UISocialWindow.opensSocialWindow(GameManager.SOCIAL_TYPE_FRIEND);
				break;
			case ID_MAP:// 地图
				UIMenu.openWorldMap();
				break;
			case ID_SIDE_PLAYERS:// 附近玩家
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "暂不开启");
				break;
			case ID_MYTEAM:// 我的队伍
				GameManager.getInstance().m_game_message.sendTeamAllMsg();
				UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_ID_TEAM);
				break;
			case ID_YUANBAO_MARKET://
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "暂不开启");
				break;
			case ID_YUANBAO_CHONGZHI://
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "暂不开启");
				break;
			case ID_HUODONG://
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "暂不开启");
				break;
			case ID_AUCTION://
				UIAuction.openMenu();
				break;
			case ID_SET_SHORTCUT://
				UISetShortCut.openSetMenu();
				break;
			case ID_LEARN_SKILL:
				UISkill.gotoSkill();
				break;//
			case ID_SIDE_TEAM:
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "暂不开启");
				break;//
			case ID_JOIN_TEAM:
				GameManager.getInstance().m_game_message
						.sendTeamRequest(GameManager.getInstance().m_role.m_targetActor.m_gameID);
				break;//
			case ID_ADD_FRIEND:
				GameManager.getInstance().m_game_message.sendSocialAddForName(
						GameManager.SOCIAL_TYPE_FRIEND,
						GameManager.getInstance().m_role.m_targetActor.m_name);
				break;//
			case ID_ADD_ENEMY:
				GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_B, "暂不开启");
				break;//
			case ID_VIEWPRIORITY:
				if (GameManager.s_system_infos[GameManager.SET_DIS_PLAYERS] < 2) {
					GameManager.s_system_infos[GameManager.SET_DIS_PLAYERS]++;
				}
				else {
					GameManager.s_system_infos[GameManager.SET_DIS_PLAYERS] = 0;
				}
				//提示
				switch (GameManager.s_system_infos[GameManager.SET_DIS_PLAYERS]) {
					case 0://自己
						GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "所有玩家不显示");
						break;
					case 1:
						GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "显示部分玩家");
						break;
					case 2:
						GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "显示所有玩家");
						break;
				}
				break;
			case ID_RIDE:
				if (GameManager.getInstance().m_role.m_isride) {
					if ( GameManager.getInstance().m_role.m_issendride == -1 ) {
						GameManager.getInstance().m_role.m_issendride = 1;
						GameManager.getInstance().m_role.zqIsUseRide(false, null, (byte) -1);
						GameManager.getInstance().m_game_message.sendRideClose();
					}
				}else{
					GameManager.getInstance().m_role.ride() ;
				}
				break;
			case ID_MARCO1:
			case ID_MARCO2:
			case ID_MARCO3:
			case ID_MARCO4:
				ActorRole role = GameManager.getInstance().m_role;
				if (role.isAliveStatus()) {
					GameMacro[] gms = GameManager.getInstance().m_role.m_game_macro;
					if (gms != null && id - ID_MARCO1 < gms.length) {
						GameMacro gm = gms[(int) (id - ID_MARCO1)];
						if (gm != null && !gm.checkMacroNull()) {
							if (role.m_targetActor != null && !role.checkTargetFriend(role.m_targetActor)) {
								gm.init();
								GameManager.getInstance().m_role.m_macro_index = (byte) (id - ID_MARCO1);
								GameManager.getInstance().m_role.m_macro_open = true;
							}
						}
						else {
							GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "请先设置连招!");
						}
					}
				}
				break;
			case ID_GONGLUE:
				GameManager.getInstance().m_game_message.sendGonglue();
				break;
			case ID_ONE_KEY_TASK:
				GameManager.getInstance().m_game_message.sendOneKeyTask();
				break;
		}
	}
}
